package com.zlm.controller;


import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        System.out.println("hello security");
        // 1.获取认证信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        // 这个User是SpringSecurity里面自带的User对象
        User user = (User) authentication.getPrincipal();
        System.out.println("身份信息：" + user);
        System.out.println("姓名信息：" + user.getUsername());
        System.out.println("权限信息：" + authentication.getAuthorities());
        System.out.println("凭证信息：" + authentication.getCredentials());

        // 默认的策略模式是子线程没有办法得到用户信息的
        new Thread(() -> {
            Authentication authentication1 = SecurityContextHolder.getContext().getAuthentication();
            System.out.println("子线程的用户信息：" + authentication1);
        }).start(); // 开始执行子线程

        return "hello security";
    }
}
